
<?php
class TestController extends Controller {

	private $rootPath = "";		///var/www/html/howdo/
	private $targetPath = "common/upload/";
	private $uploadpath;
	public function __construct() {
		parent::__construct ();
		$this->view->web_host = $this->getRequest ()->hostUrl;
		$this->view->web_app_url = $this->getRequest ()->hostUrl . "/clientapi.php";
		$this->uploadpath=$this->rootPath.$this->targetPath;
	}
	//test
	public function Index(){
		$this->view->display("index.html");
	}
	public function Index1(){
		//加载上传类
		$this->getApp()->loadUtilClass("UploadUtil");
		//设置上传类的参数（参数为数组）
		$upPara=array(
				'allowType'=>array("acc","mp3","wav","amr","png","jpg","json"),
				'maxsize'=>"200000000",
				'israndname'=>true,
				'filepath'=>$this->uploadpath."audio/"
		);
		$up=new uploadutil($upPara);
		$upAudio=$up->uploadFile("image");
		if ($upAudio){
			echo $up ->getErrorMsg();
			echo "德斌欠我一个红苹果";
		}else {
			echo $up ->getErrorMsg();
			echo "德斌bu欠我一个红苹果";
		}
	}
	
	public function Uplength(){
		$size = filesize( $this->uploadpath . "test.html");
		if ($size){
			echo "文件大小为".$size;
		}else {
			echo 0;
		}
		
	}
	public function Upstring(){
		$file_content = $this->getRequest ()->get ( "file_content" );
		$file_size = $this->getRequest ()->get ( "file_size" );
		$file_name = $this->getRequest ()->get ( "file_name" );
		echo "内容".$file_content;
		echo "<p>";
		echo "大小".$file_size;
		echo "<p>";
		echo "名称".$file_name;
		echo "<p>";
		$filename = "test.html";
		$fp = fopen ( $this->uploadpath . $filename, 'a+' );
		fwrite ( $fp, $file_content );
		fclose ( $fp );
		$size = filesize( $this->uploadpath . $filename);
		$data["server"]=implode('',$_SERVER);
		$data["post"]=implode('',$_POST);
		$data["get"]=implode('',$_GET);
		if ($size){
			var_dump($data);
			echo "<p>";
			echo "文件大小为".$size;
			echo "<p>";
			
		}else {
			echo 0;
		}
		
	}
	
	/**
	 * 上传轨迹
	 */
	public function UploadTrail() {
		$fileTypes = array (
				'txt'
		);
		$trail = $_FILES ["trail"] ["name"];
		if ($_FILES ["trail"] ["error"] > 0) {
			switch ($_FILES ["trail"] ["error"]) {
				case 1 : // UPLOAD_ERR_INI_SIZE
				case 2 : // UPLOAD_ERR_FORM_SIZE
					$this->view->setMsg ( "上传失败: 文件大小超过限制~" );
					break;
				case 3 : // UPLOAD_ERR_PARTIAL
					$this->view->setMsg ( "上传失败: 文件只有部分被上传~" );
					break;
				case 4 : // UPLOAD_ERR_NO_FILE
					$this->view->setMsg ( "上传失败: 没有发现文件~" );
					break;
				case 5 :
					$this->view->setMsg ( "上传失败: 文件大小为0~" );
					break;
				default :
					$this->view->setMsg ( "上传失败: " . $_FILES ["trail"] ["error"] );
					break;
			}
			$data = ( object ) null;
			$this->view->setState ( "0" );
		} else {
			$fileParts = pathinfo ( $trail );
			if (in_array ( strtolower ( $fileParts ['extension'] ), $fileTypes )) {
				$targetPath = "common/upload/audio/";
				if (move_uploaded_file ( $_FILES ['trail'] ["tmp_name"], $targetPath . $trail )) {
					$linktrail = $this->view->web_host . "/" . $targetPath . $trail;
					
						$this->view->setState ( "1" );
						$this->view->setMsg ( "上传轨迹成功~" );
				} else { // 移动位置失败
					$this->view->setState ( "0" );
					$this->view->setMsg ( "移动位置失败,上传轨迹失败~" );
				}
			} else {
				$this->view->setState ( "0" );
				$this->view->setMsg ( "文件格式有误，只支持上传txt哦~" );
			}
		}
		$this->view->setData (  $_FILES );
		$this->view->setStatus ( "1" );
		$this->view->display ( "json" );
	}
	
	public function Index3(){
		$newfilename = $_FILES ["image"] ["name"];
		$qiniu = $this->getApp()->loadUtilClass("QiniuUtil");
		$qiniu_list = $this->getApp()->loadConf("qiniu");
		$qiniu->set_conf($qiniu_list['accessKey'],$qiniu_list['secretKey'],$qiniu_list['bucket_images']);
		$qiniu->upload_files ( $newfilename , $_FILES ['image'] ["tmp_name"]);
			
	}
	
	public function Index4(){
		$newfilename = $_FILES ["image"] ["name"];
		$qiniu = $this->getApp()->loadUtilClass("QiniuUtil");
		$qiniu_list = $this->getApp()->loadConf("qiniu");
		$qiniu->set_conf($qiniu_list['accessKey'],$qiniu_list['secretKey'],$qiniu_list['bucket_video']);
		$qiniu->get_token ( $newfilename , $_FILES ['image'] ["tmp_name"]);
			
	}
	
	public function Index2(){
		//加载上传类
		$this->getApp()->loadUtilClass("UploadUtil");
		//设置上传类的参数（参数为数组）
		$upPara=array(
				'allowType'=>array("acc","json"),
				'maxsize'=>"2000000",
				'israndname'=>true,
				'filepath'=>$this->uploadpath."video/"
		);
		$up=new uploadutil($upPara);
		$upTrail=$up->uploadFile("trail");
		$upAudio=$up->uploadFile("audio");
		if ($upTrail && $upAudio){
			echo "德斌欠我一个红苹果";
		}else {
			echo "德斌bu欠我一个红苹果";
		}
	}
}
